<template>
	<view>
		<view>
		<!-- 1忘记登录密码 ！2忘记支付密码 	-->
		<u-field
			v-model="form.phone"
			label="手机号"
			placeholder="请填写手机号"
		>
		</u-field>
		<u-field
			v-model="form.code"
			label="验证码"
			placeholder="请填写验证码"
		>
			<u-button size="mini" slot="right" type="warning" @click="getCode" v-if="showText">{{form.codeText}}</u-button>
			<u-button size="mini" slot="right" type="warning"  v-if="!showText">{{second}}秒</u-button>
		</u-field>
		<u-field
			:password="true"
			v-model="form.password"
			label="新密码"
			placeholder="请填写新密码"
		>
		</u-field>
		<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
		<view style="width: 95%; margin:40px auto; border-radius: 30px; background: #FA9700; color: #fff; line-height: 46px; text-align: center; " @click="changePass">
			确定
		</view>
		</view>
	</view>
</template>

<script>
	import {pass,sendSms,checkCode,setPayPwd} from "@/common/config/request.js"
	export default {
		data() {
			return {
				form:{
					type:"",
					codeText:'',
					phone:"",//手机号
					password:"",//密码
					code:"",//验证码
				},
				second: 60, //默认60秒
				showText: true, //判断短信是否发送
			}
		},
		onLoad(e){
			this.form.type=e.type
			this.form.phone=uni.getStorageSync('mobile')
		},
		methods: {
			codeChange(text) {
				console.log(text,'2222')
				this.form.codeText = text;
			},
			changePass(){
				// 忘记密码
				// console.log('22222',this.form)
				
				if(this.form.type==1){
					let data={
						phone:this.form.phone,
						pwd:this.form.password,
						code:this.form.code
					}
					pass(data).then((res)=>{
						console.log(res)
						if(res.data.code==1){
							uni.showToast({ title: '修改成功', icon: 'none' });
							this.form.phone=""
							this.form.code=""
							this.form.password=""
						}
					})
				}
				// 忘记支付密码
				if(this.form.type==2){
					let code={
						phone:this.form.phone,
						code:this.form.code
					}
					checkCode(code).then(res=>{
						console.log(res,'短信验证')
						if(res.data.code==1){
							let dates={
								type:1,
								pwd:this.form.password,
							}
							setPayPwd(dates).then(res=>{
								// console.log()
								if(res.data.code==1){
									uni.showToast({
										icon:'none',
										title:'设置成功'
									})
									this.from.password=''
								}
							})
						}
					})
				}
			},
			djs(){
				var that = this;
				var interval = setInterval(() => {
					that.showText = false;
					var times = that.second - 1;
					//that.second = times<10?'0'+times:times ;//小于10秒补 0
					that.second = times;
					console.log(times);
				}, 1000);
				setTimeout(() => {
					clearInterval(interval);
					that.second = 60;
					that.showText = true;
				}, 60000);
			},
			yzphone(){
				return new Promise((r,n)=>{
					if (!this.form.phone) {
						uni.showToast({ title: '请输入手机号', icon: 'none' });
						return;
					}
					if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.form.phone)) {
						uni.showToast({ title: '请输入正确手机号', icon: 'none' });
						return;
					}
					r(true)
				})
			},
			//获取短信验证码
			getCode() {
				this.yzphone().then((r)=>{
					if(r==true){
						sendSms({phone:this.form.phone}).then((res)=>{
							console.log(res)
							if(res.data.code==1){
								this.djs()
							}
							
						})
					}
				})
				
				
				//这里请求后台获取短信验证码
				
				
				// uni.request({
				// 	//......//此处省略
				// 	success: function(res) {
				// 		that.showText = false;
				// 	}
				// });
			},
			// getCode() {
			// 	if(this.$refs.uCode.canGetCode) {
			// 		// 模拟向后端请求验证码
			// 		uni.showLoading({
			// 			title: '正在获取验证码'
			// 		})
			// 		setTimeout(() => {
			// 			uni.hideLoading();
			// 			// 通知验证码组件内部开始倒计时
			// 			this.$refs.uCode.start();
			// 		}, 1000);
			// 	}else {
			// 		this.$u.toast('倒计时结束后再发送');
			// 	}
			// }
		}
	}
</script>

<style>

</style>
